package com.lxm.shownews.commons;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lxm on 2016/2/1.
 */
public class DBOpenHelper extends SQLiteOpenHelper {

    private final static String DB_NAME = "db_channel.db";
    private final static int VERSION = 1;
    private SQLiteDatabase db = null;

    public DBOpenHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
        getConnection();
    }

    public SQLiteDatabase getSQLiteDatabase() {
        return db;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE channel_info(_id INTEGER PRIMARY KEY AUTOINCREMENT,channelId, name, sortNum INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            db.execSQL("DROP TABLE IF EXISTS channel_info");
            onCreate(db);
        }
    }

    public void getConnection() {
        db = this.getReadableDatabase();
    }

    public Cursor selectCursor(String sql, String[] selectionArgs) {
        return db.rawQuery(sql, selectionArgs);
    }


    public int selectCount(String sql, String[] selectionArgs) {
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        if (cursor != null) {
            int count = cursor.getCount();
            cursor.close();
            return count;
        }
        return 0;
    }

    /**
     * @param sql
     * @param selectionArgs
     * @return
     */
    public List<Map<String, Object>> selectData(String sql, String[] selectionArgs) {
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        return cursorToList(cursor);
    }

    /**
     * @param sql
     * @param selectionArgs
     * @return
     */
    public boolean updateData(String sql, Object[] selectionArgs) {
        try {
            db.execSQL(sql, selectionArgs);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public List<Map<String, Object>> cursorToList(Cursor cursor) {
        if (cursor == null) {
            return null;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        String[] columnNames = cursor.getColumnNames();
        while (cursor.moveToNext()) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < columnNames.length; i++) {
                int type = cursor.getType(i);
                Object value = null;
                switch (type) {
                    case Cursor.FIELD_TYPE_STRING:
                        value = cursor.getString(i);
                        break;
                    case Cursor.FIELD_TYPE_BLOB:
                        value = cursor.getBlob(i);
                        break;
                    case Cursor.FIELD_TYPE_FLOAT:
                        value = cursor.getFloat(i);
                        break;
                    case Cursor.FIELD_TYPE_INTEGER:
                        value = cursor.getInt(i);
                        break;

                    default:
                        value = null;
                        break;
                }
                map.put(columnNames[i], value);
            }
            list.add(map);

        }
        cursor.close();
        return list;
    }

    public void destroy() {
        if (db != null) {
            db.close();
        }
    }


}
